import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createStyleImportPlugin } from 'vite-plugin-style-import'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

export default defineConfig({
    plugins: [
        vue(),
        vueDevTools(),
        AutoImport({
            // 引用模块
            imports: [ 'vue', 'vue-router', '@vueuse/core', 'pinia' ],
            // 配置文件
            dts: 'src/common/types/auto-imports.d.ts',
            // 默认扫描目录
            dirs: [ 'src/common' ],
            // Eslint配置
            eslintrc: {
                // 文件生成之后，关闭即可，避免重复生成，导致Eslint报错
                enabled: false,
                filepath: 'src/common/types/.eslintrc-auto-import.json',
                globalsPropValue: true
            },
            resolvers: [
                ElementPlusResolver(),
                IconsResolver({
                    prefix: 'i'
                })
            ]
        }),
        Components({
            // 配置文件
            dts: 'src/common/types/components.d.ts',
            // 默认扫描目录
            dirs: [ 'src/common' ],
            // 忽略目录
            globalNamespaces: [],
            resolvers: [
                IconsResolver({
                    // 前缀为 `i`
                    prefix: 'i',
                    // 明确启用的图标集
                    enabledCollections: [ 'ep' ]
                }),
                ElementPlusResolver() ]
        }),
        createStyleImportPlugin({
            libs: [ {
                libraryName: 'element-plus',
                resolveStyle: (name) => `element-plus/es/components/${name}/style/css`
            } ]
        }),
        Icons({
            autoInstall: true,
            compiler: 'vue3'
        })
    ],
    css: {
        preprocessorOptions: {
            less: {
                charset: false,
                additionalData: `@import "./src/resources/styles/global.less";`
            }
        }
    },
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    },
    server: {
        host: 'localhost',
        port: 12001,
        strictPort: true,
        open: true
    },
    build: {}
})
